//
//  UIViewController+MSAlertController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/2.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "UIViewController+MSAlertController.h"

@implementation UIViewController (MSAlertController)

- (void)showAlertControllerWithTitle:(NSString *)title
                             message:(NSString *)message
                       confirmButton:(NSString *)confirmButton
                        cancelButton:(NSString *)cancelButton
                   confirmClickBlock:(void (^)(void))clickBlock
                    cancelClickBlock:(void (^)(void))cancelBlock {
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (![MSSafeObject isNull:cancelButton]) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButton?:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (cancelBlock) {
                cancelBlock();
            }
        }];
        [alertController addAction:cancelAction];
    }
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmButton?:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (clickBlock) {
            clickBlock();
        }
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)showActionSheetWithTitle:(NSString *)title
                         message:(NSString *)message
                    buttonTitles:(NSArray *)buttonTitles
                    cancelButton:(NSString *)cancelButton
                       clickView:(UIButton *)clickView
               confirmClickBlock:(void (^)(NSInteger))clickBlock
                     cancelBlock:(void (^)(void))cancelBlock {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    for (NSString *btnTitle in buttonTitles) {
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:btnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (clickBlock) {
                clickBlock([buttonTitles indexOfObject:btnTitle]);
            }
        }];
        [alertController addAction:alertAction];
    }
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButton?:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancelBlock) {
            cancelBlock();
        }
    }];
    
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
